#creation d'une liste de SNPs à eliminer
snp_selection <- function(inbfile=NULL,HW=NULL, miss=NULL, MAF=NULL, del_AT_GC=F, del_chrXYMT=F, ATGCfile="delAT-GC.txt",outputfile="SNPtoDel.txt"){
	if(!is.null(inbfile)){

		tmp_list <- NULL

		# Création d'une liste de SNPs sur les chromosomes X, Y et MT
		if(del_chrXYMT==T){
			chrXYMT <- read.table(file=paste(inbfile,".bim",sep=""))
			chrXYMT2 <- chrXYMT[chrXYMT$V1 > 22,"V2"] 
			write.table(as.data.frame(chrXYMT2),file="chrXYMT.txt",quote=F,row.names=F) 
			cat("see heterosomes'list : chrXYMT.txt \n")
			tmp_list <- chrXYMT2
		}
		
		# creation d’un fichier de desequilibre de Hardy Weinberg (hwe.log, hwe.hh, hwe.hwe)
		system(paste("plink --noweb --bfile ",inbfile," --hardy --out hwe",sep=""))

		# creation d'un fichier de difference de call rate (% de genotypage) entre cas et contrôles (dmiss.log, dmiss.hh, dmiss.missing)
		system(paste("plink --noweb --bfile ",inbfile," --test-missing --out dmiss",sep=""))

		# creation d'un fichier de frequence allelique (maf.log, maf.hh, maf.frq)
		system(paste("plink --noweb --bfile ",inbfile," --freq --out maf",sep=""))
	
		# mise en commun des resultats 
		hwe <- read.table(file="hwe.hwe",h=T)
		dmiss <- read.table(file="dmiss.missing",h=T)
		maf <-  read.table(file="maf.frq",h=T)

		data <- merge(hwe,dmiss, by=c("CHR","SNP"))
		names(data)[9] <- "P.HW"
		names(data)[12] <- "P.dmiss"
		data <-merge(data,maf, by=c("CHR", "SNP", "A1", "A2"))	

		# suppression A/T, T/A, G/C et C/G
		if(del_AT_GC==T){
			data$bin <-ifelse((data$A1=="A" & data$A2=="T"),0,1) # delete A/T
			sum2 <- data[data$bin==1,]
			delATCG <- as.character(data[data$bin==0,"SNP"])
			sum2$bin <-ifelse((sum2$A1=="T" & sum2$A2=="A"),0,1) # delete T/A
			sum3 <- sum2[sum2$bin==1,]
			delATCG <- c(delATCG,as.character(sum2[sum2$bin==0,"SNP"])) 
			sum3$bin <-ifelse((sum3$A1=="C" & sum3$A2=="G"),0,1) # delete C/G
			sum4 <- sum3[sum3$bin==1,]
			delATCG <- c(delATCG,as.character(sum3[sum3$bin==0,"SNP"]))
			sum4$bin <-ifelse((sum4$A1=="G" & sum4$A2=="C"),0,1) # delete G/C
			sum5 <- sum4[sum4$bin==1,]
			delATCG <- c(delATCG,as.character(sum4[sum4$bin==0,"SNP"]))
			write.table(unique(delATCG),file=ATGCfile,quote=F,row.names=F)
			ifelse(!is.null(tmp_list), tmp_list <- c(tmp_list,delATCG), tmp_list <- delATCG)
#			data <- sum5[-c(15)]
		}

		fam <- read.table(file=paste(inbfile,".fam",sep=""))
		study <- length(unique(fam$V6)) # study = 1 (cases or controls), study = 2 (cases and controls) 

		if(study == 1){
			if(unique(fam$V6) == 1){data2 <- data[data$TEST=="UNAFF",-c(10)]; names(data2)[10] <- "F_MISS"}
			if(unique(fam$V6) == 2){data2 <- data[data$TEST=="AFF",-c(11)]; names(data2)[10] <- "F_MISS"}

			cat("create SNPs list to be deleted : \n")
		
			# elimination des SNPs HW < HW
			if(!is.null(HW)){
				tmp_data2 <- data2[data2$P.HW < HW & !is.na(data2$P.HW),] 
				# fusion des donnees
				allSNP <- c(as.character(tmp_data2$SNP))
				cat("add SNPs (p-value hwe < ",HW,")\n",sep="")
			}
	
			# elimination des SNPs avec un % de missing  > miss
			if(!is.null(miss)){
				tmp_data3 <- data2[data2$F_MISS > miss & !is.na(data2$F_MISS),]
				allSNP <- c(allSNP,as.character(tmp_data3$SNP))
				cat("add SNPs (missing < ",miss,")\n",sep="")
			}

			# elimination des SNPs avec MAF < maf
			if(!is.null(MAF)){
				tmp_data4 <- data2[data2$MAF < MAF & !is.na(data2$MAF),]
				allSNP <- c(allSNP,as.character(tmp_data4$SNP))
				cat("add SNPs (minor allele frequency < ",MAF,")\n",sep="")
			}
		}
		else if(study == 2){
			aff <- data[data$TEST=="AFF",]
			unaff <- data[data$TEST=="UNAFF",]
			tmp <- merge(aff,unaff,by=c("CHR","SNP"))
			allSNP <- NULL	
		
			cat("create SNPs list to be deleted : \n")
		
			# elimination des SNPs HW < HW
			if(!is.null(HW)){
				tmp_aff <- tmp[tmp$P.HW.x < HW & !is.na(tmp$P.HW.x),"SNP"] 
				tmp_unaff <- tmp[tmp$P.HW.y < HW & !is.na(tmp$P.HW.y),"SNP"]
				# fusion des donnees
				allSNP <- c(as.character(tmp_aff),as.character(tmp_unaff))
				cat("add SNPs (p-value hwe < ",HW,")\n",sep="")
			}
	
			# elimination des SNPs avec un % de missing  > miss
			if(!is.null(miss)){
				tmp_aff2 <- tmp[tmp$F_MISS_A.x > miss & !is.na(tmp$F_MISS_A.x),"SNP"]
				tmp_unaff2 <- tmp[tmp$F_MISS_U.y > miss & !is.na(tmp$F_MISS_U.y),"SNP"]
				allSNP <- c(allSNP,as.character(tmp_aff2),as.character(tmp_unaff2))
				cat("add SNPs (missing < ",miss,")\n",sep="")
			}

			# elimination des SNPs avec MAF < maf
			if(!is.null(maf)){
				tmp_aff3 <- tmp[tmp$MAF.x < MAF & !is.na(tmp$MAF.x),"SNP"]
				tmp_unaff3 <- tmp[tmp$MAF.y < MAF & !is.na(tmp$MAF.y),"SNP"]
				allSNP <- c(allSNP,as.character(tmp_aff3),as.character(tmp_unaff3))
				cat("add SNPs (minor allele frequency < ",MAF,")\n",sep="")
			}
		}

		# concaténation des données et suppression des doublons
		ifelse(!is.null(tmp_list),final_list <- c(tmp_list, allSNP), final_list <- allSNP)
		final_list <- unique(final_list)

		# enregistrement des donnees
		write.table(final_list,file=outputfile,quote=F, row.names=F)
		cat(paste("see results : ", outputfile,"\n",sep=""))

	}
	else { cat("missing inbfile parameter.\n")}
}
